﻿namespace KeRui.BLL
{
    using KeRui.Common;
    using KeRui.DAL;
    using KeRui.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;

    public class CmsContent
    {
        private readonly KeRui.DAL.CmsContent dal = new KeRui.DAL.CmsContent();

        public int Add(KeRui.Model.CmsContent model)
        {
            if (string.IsNullOrEmpty(model.CreateIP))
            {
                model.CreateIP = Utils.GetRealIP();
            }
            return this.dal.Add(model);
        }

        public void AddHits(int ContentID)
        {
            this.dal.AddHits(ContentID);
        }

        public void AppendContent(int contentID, string content)
        {
            this.dal.AppendContent(contentID, content);
        }

        public void Check(int contentID)
        {
            this.dal.Check(contentID);
        }

        public List<KeRui.Model.CmsContent> DataTableToList(DataTable dt)
        {
            List<KeRui.Model.CmsContent> list = new List<KeRui.Model.CmsContent>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    KeRui.Model.CmsContent item = new KeRui.Model.CmsContent();
                    if (dt.Rows[i]["ContentID"].ToString() != "")
                    {
                        item.ContentID = int.Parse(dt.Rows[i]["ContentID"].ToString());
                    }
                    if (dt.Rows[i]["NodeID"].ToString() != "")
                    {
                        item.NodeID = int.Parse(dt.Rows[i]["NodeID"].ToString());
                    }
                    item.NodeName = dt.Rows[i]["NodeName"].ToString();
                    if (dt.Rows[i]["ModelID"].ToString() != "")
                    {
                        item.ModelID = int.Parse(dt.Rows[i]["ModelID"].ToString());
                    }
                    if (dt.Rows[i]["ContentType"].ToString() != "")
                    {
                        item.ContentType = int.Parse(dt.Rows[i]["ContentType"].ToString());
                    }
                    if (dt.Rows[i]["Summary"].ToString() != "")
                    {
                        item.Summary = dt.Rows[i]["Summary"].ToString();
                    }
                    item.Title = dt.Rows[i]["Title"].ToString();
                    item.TitleStyle = dt.Rows[i]["TitleStyle"].ToString();
                    if (dt.Rows[i]["IsRecommend"].ToString() != "")
                    {
                        if ((dt.Rows[i]["IsRecommend"].ToString() == "1") || (dt.Rows[i]["IsRecommend"].ToString().ToLower() == "true"))
                        {
                            item.IsRecommend = true;
                        }
                        else
                        {
                            item.IsRecommend = false;
                        }
                    }
                    if (dt.Rows[i]["IsHot"].ToString() != "")
                    {
                        if ((dt.Rows[i]["IsHot"].ToString() == "1") || (dt.Rows[i]["IsHot"].ToString().ToLower() == "true"))
                        {
                            item.IsHot = true;
                        }
                        else
                        {
                            item.IsHot = false;
                        }
                    }
                    if (dt.Rows[i]["IsMarked"].ToString() != "")
                    {
                        if ((dt.Rows[i]["IsMarked"].ToString() == "1") || (dt.Rows[i]["IsMarked"].ToString().ToLower() == "true"))
                        {
                            item.IsMarked = true;
                        }
                        else
                        {
                            item.IsMarked = false;
                        }
                    }
                    if (dt.Rows[i]["IsTop"].ToString() != "")
                    {
                        if ((dt.Rows[i]["IsTop"].ToString() == "1") || (dt.Rows[i]["IsTop"].ToString().ToLower() == "true"))
                        {
                            item.IsTop = true;
                        }
                        else
                        {
                            item.IsTop = false;
                        }
                    }
                    if (dt.Rows[i]["ContentState"].ToString() != "")
                    {
                        item.ContentState = int.Parse(dt.Rows[i]["ContentState"].ToString());
                    }
                    if (dt.Rows[i]["PublicState"].ToString() != "")
                    {
                        item.PublicState = int.Parse(dt.Rows[i]["PublicState"].ToString());
                    }
                    if (dt.Rows[i]["PublicStartTime"].ToString() != "")
                    {
                        item.PublicStartTime = DateTime.Parse(dt.Rows[i]["PublicStartTime"].ToString());
                    }
                    if (dt.Rows[i]["PublicEndTime"].ToString() != "")
                    {
                        item.PublicEndTime = DateTime.Parse(dt.Rows[i]["PublicEndTime"].ToString());
                    }
                    item.TemplateFile = dt.Rows[i]["TemplateFile"].ToString();
                    item.FilePath = dt.Rows[i]["FilePath"].ToString();
                    item.FileDir = dt.Rows[i]["FileDir"].ToString();
                    if (dt.Rows[i]["FileUpdateTime"].ToString() != "")
                    {
                        item.FileUpdateTime = DateTime.Parse(dt.Rows[i]["FileUpdateTime"].ToString());
                    }
                    if (dt.Rows[i]["Hits"].ToString() != "")
                    {
                        item.Hits = int.Parse(dt.Rows[i]["Hits"].ToString());
                    }
                    if (dt.Rows[i]["EnableComment"].ToString() != "")
                    {
                        if ((dt.Rows[i]["EnableComment"].ToString() == "1") || (dt.Rows[i]["EnableComment"].ToString().ToLower() == "true"))
                        {
                            item.EnableComment = true;
                        }
                        else
                        {
                            item.EnableComment = false;
                        }
                    }
                    if (dt.Rows[i]["CommentCount"].ToString() != "")
                    {
                        item.CommentCount = int.Parse(dt.Rows[i]["CommentCount"].ToString());
                    }
                    if (dt.Rows[i]["PrevID"].ToString() != "")
                    {
                        item.PrevID = int.Parse(dt.Rows[i]["PrevID"].ToString());
                    }
                    if (dt.Rows[i]["NextID"].ToString() != "")
                    {
                        item.NextID = int.Parse(dt.Rows[i]["NextID"].ToString());
                    }
                    if (dt.Rows[i]["ContentPages"].ToString() != "")
                    {
                        item.ContentPages = int.Parse(dt.Rows[i]["ContentPages"].ToString());
                    }
                    if (dt.Rows[i]["UserID"].ToString() != "")
                    {
                        item.UserID = int.Parse(dt.Rows[i]["UserID"].ToString());
                    }
                    item.Username = dt.Rows[i]["Username"].ToString();
                    if (dt.Rows[i]["IsDelete"].ToString() != "")
                    {
                        if ((dt.Rows[i]["IsDelete"].ToString() == "1") || (dt.Rows[i]["IsDelete"].ToString().ToLower() == "true"))
                        {
                            item.IsDelete = true;
                        }
                        else
                        {
                            item.IsDelete = false;
                        }
                    }
                    if (dt.Rows[i]["UpdateTime"].ToString() != "")
                    {
                        item.UpdateTime = DateTime.Parse(dt.Rows[i]["UpdateTime"].ToString());
                    }
                    item.CreateIP = dt.Rows[i]["CreateIP"].ToString();
                    if (dt.Rows[i]["CreateTime"].ToString() != "")
                    {
                        item.CreateTime = DateTime.Parse(dt.Rows[i]["CreateTime"].ToString());
                    }
                    item.Guid = dt.Rows[i]["Guid"].ToString();
                    if (dt.Rows[i]["PublishTime"].ToString() != "")
                    {
                        item.PublishTime = DateTime.Parse(dt.Rows[i]["PublishTime"].ToString());
                    }
                    list.Add(item);
                }
            }
            return list;
        }

        public void Delete(int contentID)
        {
            this.dal.Delete(contentID);
        }

        public void DeleteByNodeID(int nodeID)
        {
            this.dal.DeleteByNodeID(nodeID);
        }

        public int FixPreNextID(int contentID, string action)
        {
            return this.dal.FixPreNextID(contentID, action);
        }

        public DataTable GetCmsContentDetailsByNodeID(int nodeID, string top, string strWhere, string indexRange)
        {
            string cacheKey = string.Concat(new object[] { "CmsContentDetailsByNodeID-", nodeID, "-", top, "-", strWhere, "-", indexRange });
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.GetCmsContentDetailsByNodeIDNotByCache(nodeID, top, strWhere, indexRange);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (DataTable) cache;
        }

        public DataTable GetCmsContentDetailsByNodeID(int nodeID, string top, string strWhere, string indexRange, string orderby)
        {
            string cacheKey = string.Concat(new object[] { "CmsContentDetailsByNodeID-", nodeID, "-", top, "-", strWhere, "-", indexRange, "-", orderby });
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.GetCmsContentDetailsByNodeIDNotByCache(nodeID, top, strWhere, indexRange, orderby);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (DataTable) cache;
        }

        public DataTable GetCmsContentDetailsByNodeIDNotByCache(int nodeID, string top, string strWhere, string indexRange)
        {
            return this.dal.GetCmsContentDetailsByNodeID(nodeID, top, strWhere, indexRange, "ORDER BY cc.ContentID DESC");
        }

        public DataTable GetCmsContentDetailsByNodeIDNotByCache(int nodeID, string top, string strWhere, string indexRange, string orderby)
        {
            return this.dal.GetCmsContentDetailsByNodeID(nodeID, top, strWhere, indexRange, orderby);
        }

        public DataTable GetCmsContentDetailsByParentNodeID(int parentNodeID, string top, string strWhere, string indexRange)
        {
            string cacheKey = string.Concat(new object[] { "CmsContentSearchList-", parentNodeID, "-", top, "-", strWhere, "-", indexRange });
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.GetCmsContentDetailsByParentNodeIDNotByCache(parentNodeID, top, strWhere, indexRange);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (DataTable) cache;
        }

        public DataTable GetCmsContentDetailsByParentNodeID(int parentNodeID, string top, string strWhere, string indexRange, string orderby)
        {
            string cacheKey = string.Concat(new object[] { "CmsContentDetailsByParentNodeID-", parentNodeID, "-", top, "-", strWhere, "-", indexRange, "-", orderby });
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.GetCmsContentDetailsByParentNodeIDNotByCache(parentNodeID, top, strWhere, indexRange, orderby);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (DataTable) cache;
        }

        public DataTable GetCmsContentDetailsByParentNodeIDNotByCache(int parentNodeID, string top, string strWhere, string indexRange)
        {
            return this.dal.GetCmsContentDetailsByParentNodeID(parentNodeID, top, strWhere, indexRange, "ORDER BY cc.ContentID DESC");
        }

        public DataTable GetCmsContentDetailsByParentNodeIDNotByCache(int parentNodeID, string top, string strWhere, string indexRange, string orderby)
        {
            return this.dal.GetCmsContentDetailsByParentNodeID(parentNodeID, top, strWhere, indexRange, orderby);
        }

        public DataTable GetCmsContentRelativeNews(int nodeID, string type, int top, string dateRange)
        {
            return this.dal.GetCmsContentRelativeNews(nodeID, type, top, dateRange);
        }

        public DataTable GetContentIDByNodeID(int NodeID)
        {
            return this.dal.GetContentIDByNodeID(NodeID);
        }

        public int GetCount(string strWhere)
        {
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            return common.GetCount("CmsContent", strWhere);
        }

        public int GetCountByNodeID(int nodeID)
        {
            return this.dal.GetCountByNodeID(nodeID);
        }

        public int GetCountByParentNodeID(int nodeID)
        {
            return this.dal.GetCountByParentNodeID(nodeID);
        }

        public int GetCoutByNodeID(int NodeID)
        {
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            return common.GetCount("CmsContent", " IsDelete='False' AND PublicState=1  AND ContentState=2 AND NodeID=" + NodeID);
        }

        public List<KeRui.Model.CmsContent> GetList(string strWhere, int PageSize, int PageIndex)
        {
            string strOrder = "ContentID DESC";
            DataSet set = new KeRui.DAL.Common().GetList("CmsContent", PageSize, PageIndex, strWhere, strOrder);
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.CmsContent> GetListByNodeID(int PageSize, int PageIndex, int NodeID)
        {
            string strOrder = "CreateTime DESC,IsTop DESC";
            DataSet set = new KeRui.DAL.Common().GetList("CmsContent", PageSize, PageIndex, " NodeID=" + NodeID, strOrder);
            return this.DataTableToList(set.Tables[0]);
        }

        public KeRui.Model.CmsContent GetModel(int ContentID)
        {
            return this.dal.GetModel(ContentID);
        }

        public KeRui.Model.CmsContent GetModelByCache(int ContentID)
        {
            string cacheKey = "CmsContentModel-" + ContentID;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.dal.GetModel(ContentID);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (KeRui.Model.CmsContent) cache;
        }

        public DataSet GetModelByContentID(int contentID, string strWhere)
        {
            return this.dal.GetModelByContentID(contentID, strWhere);
        }

        public DataSet GetModelDetailsByContentID(int contentID)
        {
            return this.dal.GetModelDetailsByContentID(contentID);
        }

        public int GetNodeIDByNameModelID(string strNode, int modelID)
        {
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            string strWhere = string.Concat(new object[] { " 1=1 AND NodeName='", strNode, "' AND ModelID =", modelID });
            DataTable table = common.GetModel("CmsContent", strWhere).Tables[0];
            return int.Parse(table.Rows[0]["NodeID"].ToString());
        }

        public DataSet GetSetList(string strWhere, int PageSize, int PageIndex)
        {
            string strOrder = "ContentID DESC";
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            return common.GetList("CmsContent", PageSize, PageIndex, strWhere, strOrder);
        }

        public void MoveContentByContentID(string ContentIDs, int oldNode, int newNode, int num)
        {
            this.dal.MoveContentByContentID(ContentIDs, oldNode, newNode, num);
        }

        public void MoveContentByNodeID(int oldNode, int newNode)
        {
            this.dal.MoveContentByNodeID(oldNode, newNode);
        }

        public DataTable Search(string keyword)
        {
            return this.dal.Search(Utils.SqlStringFormat(keyword, 2));
        }

        public DataSet SearchList(string strWhere, int PageSize, int PageIndex)
        {
            return this.dal.SearchList(strWhere, PageSize, PageIndex);
        }

        public DataSet SearchListByCache(string strWhere, int PageSize, int PageIndex)
        {
            string cacheKey = string.Concat(new object[] { "CmsContentSearchList-", strWhere, "-", PageSize, "-", PageIndex });
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.SearchList(strWhere, PageSize, PageIndex);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (DataSet) cache;
        }

        public void Update(KeRui.Model.CmsContent model)
        {
            this.dal.Update(model);
        }

        public int UpdateFourStatus(int contentID, string type)
        {
            return this.dal.UpdateFourStatus(contentID, type);
        }
    }
}

